package com.yb.leetcode;

public class Solution7 {
    public int reverse(int x) {
        if (x == 0) {
            return 0;
        }
        String s = Integer.valueOf(x).toString();
        while (s.endsWith("0")) {
            s = s.substring(0, s.length() - 1);
        }
        if (x > 0) {
            s = new StringBuilder(s).reverse().toString();
            try {
                return Integer.parseInt(s);
            } catch (Exception e) {
                return 0;
            }
        } else {
            s = new StringBuilder(s.substring(1)).reverse().toString();
            try {
                return Integer.parseInt("-" + s);
            } catch (Exception e) {
                return 0;
            }
        }
    }
}
